/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.psdk.remote;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import com.qihoo.psdk.util.a;
import com.qihoo.psdk.util.h;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;

public class m {
    private static Socket c = null;
    private static DataInputStream d = null;
    private static OutputStream e = null;
    protected static Map a = new HashMap();
    private static int f = 0;
    protected static long b = 0L;

    private static String d() {
        return "\n*********loopNum: " + (++f) + "*********";
    }

    protected static void a() {
        k localk = null;
        Iterator localIterator = a.entrySet().iterator();
        while (localIterator.hasNext()) {
            Map.Entry localEntry = (Map.Entry) localIterator.next();
            localk = (k) localEntry.getValue();
            localk.d = false;
            localk.e = "";
        }
    }

    protected static void a(Context paramContext) {
        try {
            h.a("QRemoteSession", "Start  socket: " + n.b() + ":" + n.c() + ", ts: " + a.f());
            a();
            InetAddress localInetAddress = InetAddress.getByName(n.b());
            c = new Socket();
            c.connect(new InetSocketAddress(localInetAddress, n.c()), 60000);
            c.setSoTimeout(3600000);
            d = new DataInputStream(c.getInputStream());
            e = c.getOutputStream();
            h.a("QRemoteSession", "Finish socket: " + n.b() + ":" + n.c() + ", ts: " + a.f());
            f = 0;
            byte[] arrayOfByte1 = null;
            int i = 0;
            int j = 0;
            int k = 0;
            int l = 0;
            String str1 = "";
            k localk = null;
            while ((c != null) && (d != null)) {
                arrayOfByte1 = new byte[2];
                d.readFully(arrayOfByte1);
                i = d.a(arrayOfByte1, 0);
                arrayOfByte1 = new byte[2];
                d.readFully(arrayOfByte1);
                j = d.a(arrayOfByte1, 0);
                if (3 == j)
                    h.a("QRemoteSession", d());
                h.a("QRemoteSession", "op: " + j.a(j));
                String str2;
                Object localObject1;
                Object localObject2;
                String str3;
                if (3 == j) {
                    arrayOfByte1 = new byte[2];
                    d.readFully(arrayOfByte1);
                    k = d.a(arrayOfByte1, 0);
                    h.a("QRemoteSession", "pl: " + k);
                    if (k > 0) {
                        arrayOfByte1 = new byte[k];
                        d.readFully(arrayOfByte1);
                        str2 = new String(arrayOfByte1);
                        h.a("QRemoteSession", "ack: " + str2);
                        arrayOfByte1 = new byte[4];
                        d.readFully(arrayOfByte1);
                        l = d.b(arrayOfByte1, 0);
                        h.a("QRemoteSession", "dl: " + l);
                        localObject1 = new Vector();
                        localObject2 = null;
                        if (l > 0) {
                            arrayOfByte1 = new byte[l];
                            d.readFully(arrayOfByte1);
                            int i1 = 0;
                            byte[] arrayOfByte2 = null;
                            while (l != i1) {
                                localObject2 = new i();
                                arrayOfByte2 = new byte[8];
                                for (int i3 = 0; i3 < 8; ++i3)
                                    arrayOfByte2[i3] = arrayOfByte1[(i1++)];
                                ((i) localObject2).a = d.b(arrayOfByte2, 0);
                                arrayOfByte2 = new byte[4];
                                for (i3 = 0; i3 < 4; ++i3)
                                    arrayOfByte2[i3] = arrayOfByte1[(i1++)];
                                ((i) localObject2).b = d.b(arrayOfByte2, 0);
                                arrayOfByte2 = new byte[4];
                                for (i3 = 0; i3 < 4; ++i3)
                                    arrayOfByte2[i3] = arrayOfByte1[(i1++)];
                                ((i) localObject2).d = d.b(arrayOfByte2, 0);
                                arrayOfByte2 = new byte[((i) localObject2).d];
                                for (i3 = 0; i3 < ((i) localObject2).d; ++i3)
                                    arrayOfByte2[i3] = arrayOfByte1[(i1++)];
                                ((i) localObject2).c = new String(arrayOfByte2);
                                ((Vector) localObject1).add(localObject2);
                                h.a("QRemoteSession", "----------------");
                                h.a("QRemoteSession", "id: " + ((i) localObject2).a);
                                h.a("QRemoteSession", "appid: " + ((i) localObject2).b);
                                h.a("QRemoteSession", "messageLength: " + ((i) localObject2).d);
                                h.a("QRemoteSession", "message: " + ((i) localObject2).c);
                                h.a("QRemoteSession", "dl: " + l + ", offset: " + i1);
                            }
                        }
                        h.a("QRemoteSession", "----------------");
                        str3 = "";
                        for (int i2 = 0; i2 < ((Vector) localObject1).size(); ++i2) {
                            localObject2 = (i) ((Vector) localObject1).get(i2);
                            localk = (k) a.get(((i) localObject2).b);
                            if (localk == null)
                                continue;
                            str3 = ((k) a.get(((i) localObject2).b)).a;
                            if (!(TextUtils.isEmpty(str3))) {
                                Intent localIntent = new Intent();
                                localIntent.setAction("com.qihoo.psdk.action.notify");
                                localIntent.setPackage(str3);
                                localIntent.putExtra("targetPack", str3);
                                localIntent.putExtra("payload", ((i) localObject2).c);
                                paramContext.sendBroadcast(localIntent);
                            }
                            else {
                                h.b("QRemoteSession", "There's no packName in Route related to the appid "
                                        + ((i) localObject2).b);
                            }
                        }
                        if (!(TextUtils.isEmpty(str2))) {
                            h.a("QRemoteSession", "op: " + j.a(4));
                            a(j.a(str2));
                        }
                    }
                }
                else if (1 != j) {
                    if (6 == j) {
                        arrayOfByte1 = new byte[2];
                        d.readFully(arrayOfByte1);
                        k = d.a(arrayOfByte1, 0);
                        h.a("QRemoteSession", "pl: " + k);
                        if (k > 0) {
                            arrayOfByte1 = new byte[k];
                            d.readFully(arrayOfByte1);
                            str2 = new String(arrayOfByte1);
                            h.a("QRemoteSession", "body: " + str2);
                            localObject1 = str2.split("\n");
                            if (2 == localObject1.length) {
                                localObject2 = "";
                                str3 = "";
                                if (localObject1[0].startsWith("id:")) {
                                    localObject2 = localObject1[0];
                                    str3 = localObject1[1];
                                }
                                else {
                                    localObject2 = localObject1[1];
                                    str3 = localObject1[0];
                                }
                                str1 = ((String) localObject2).replace("id:", "");
                                h.a("QRemoteSession", "appId: " + str1 + ", r: " + str3);
                                if ("r:0".equals(str3)) {
                                    localk = (k) a.get(str1);
                                    if (localk != null) {
                                        h.a("QRemoteSession", "register ok");
                                        localk.d = true;
                                        localk.e = a.e();
                                    }
                                }
                            }
                        }
                    }
                    else if (7 == j) {
                        arrayOfByte1 = new byte[2];
                        d.readFully(arrayOfByte1);
                        k = d.a(arrayOfByte1, 0);
                        h.a("QRemoteSession", "pl: " + k);
                        if (k > 0) {
                            arrayOfByte1 = new byte[k];
                            d.readFully(arrayOfByte1);
                            str2 = new String(arrayOfByte1);
                            h.a("QRemoteSession", "body: " + str2);
                            localObject1 = str2.split("\n");
                            if (2 == localObject1.length) {
                                localObject2 = "";
                                str3 = "";
                                if (localObject1[0].startsWith("id:")) {
                                    localObject2 = localObject1[0];
                                    str3 = localObject1[1];
                                }
                                else {
                                    localObject2 = localObject1[1];
                                    str3 = localObject1[0];
                                }
                                str1 = ((String) localObject2).replace("id:", "");
                                h.a("QRemoteSession", "appId: " + str1 + ", r: " + str3);
                                if ("r:0".equals(str3)) {
                                    localk = (k) a.get(str1);
                                    if (localk != null) {
                                        h.a("QRemoteSession", "unregister ok");
                                        localk.d = false;
                                        localk.e = "";
                                        a.remove(str1);
                                    }
                                }
                            }
                        }
                    }
                }
                b = new Date().getTime();
                h.a("QRemoteSession", "---------");
            }
            c.close();
            c = null;
            d.close();
            d = null;
            e.close();
            e = null;
        } catch (Exception localException1) {
            h.c("QRemoteSession", localException1.toString() + " in initLongConnect! ts: " + a.f());
            h.a("QRemoteSession", localException1);
        } catch (Error localError) {
            h.c("QRemoteSession", localError.toString() + " in initLongConnect! ts: " + a.f());
            h.a("QRemoteSession", localError);
        } finally {
            try {
                if (c != null) {
                    c.close();
                    c = null;
                }
                if (d != null) {
                    d.close();
                    d = null;
                }
                if (e != null) {
                    e.close();
                    e = null;
                }
            } catch (Exception localException4) {
                h.a("QRemoteSession", localException4);
            }
        }
    }

    public static synchronized boolean a(byte[] paramArrayOfByte) {
        int i = 0;
        try {
            e.write(paramArrayOfByte);
            e.flush();
            i = 1;
        } catch (Exception localException1) {
            h.a("QRemoteSession", localException1);
        } catch (Error localError) {
            h.a("QRemoteSession", localError);
        } finally {
            if (i == 0)
                try {
                    if (c != null) {
                        c.close();
                        c = null;
                    }
                    if (d != null) {
                        d.close();
                        d = null;
                    }
                    if (e != null) {
                        e.close();
                        e = null;
                    }
                } catch (Exception localException4) {
                    h.a("QRemoteSession", localException4);
                }
        }
        return i;
    }

    public static boolean a(Context paramContext, String paramString1, String paramString2, String paramString3, String paramString4)
  {
    boolean bool = false;
    try
    {
      if (e != null)
      {
        k localk = (k)a.get(paramString1);
        if (localk == null)
        {
          localk = new k(paramString2, new Date().getTime(), paramString3);
          a.put(paramString1, localk);
        }
        else
        {
          localk.b = new Date().getTime();
          localk.d = false;
        }
        h.a("QRemoteSession", d());
        h.a("QRemoteSession", "op: " + j.a(2));
        bool = a(j.a(paramString3, paramString1, paramString4));
        break label176:
      }
      e.a(paramContext);
    }
    catch (Exception localException)
    {
      h.a("QRemoteSession", "ts: " + a.f() + ", Exception  in sendRegister!");
      h.a("QRemoteSession", localException);
    }
    label176: return bool;
  }

    public static boolean a(Context paramContext, String paramString1, String paramString2, String paramString3)
  {
    boolean bool = false;
    try
    {
      if (e != null)
      {
        k localk = (k)a.get(paramString1);
        if ((localk != null) && (localk.d))
        {
          localk.b = new Date().getTime();
          if (paramString2.equals(paramContext.getPackageName()))
          {
            h.a("QRemoteSession", d());
            h.a("QRemoteSession", "op: " + j.a(0));
            bool = a(j.a());
          }
          else
          {
            h.a("QRemoteSession", "op: multiplex heartbeat of " + paramString2);
            bool = true;
            break label176:
          }
        }
      }
      e.a(paramContext);
    }
    catch (Exception localException)
    {
      h.a("QRemoteSession", "ts: " + a.f() + ", Exception  in sendHeartbeat!");
      h.a("QRemoteSession", localException);
    }
    label176: return bool;
  }

    public static boolean a(Context paramContext, String paramString1, String paramString2)
  {
    boolean bool = false;
    try
    {
      if (e != null)
      {
        h.a("QRemoteSession", d());
        h.a("QRemoteSession", "op: " + j.a(5));
        bool = a(j.a(paramString1, paramString2));
        break label96:
      }
      e.a(paramContext);
    }
    catch (Exception localException)
    {
      h.a("QRemoteSession", "ts: " + a.f() + ", Exception  in sendUnreg!");
      h.a("QRemoteSession", localException);
    }
    label96: return bool;
  }

    public static void b() {
        try {
            h.b("QRemoteSession", "Reset the connect...");
            if (c != null) {
                c.close();
                c = null;
            }
            if (d != null) {
                d.close();
                d = null;
            }
            if (e == null)
                return;
            e.close();
            e = null;
        } catch (Exception localException) {
            h.a("QRemoteSession", localException);
        }
    }

    public static boolean c() {
        int i = 1;
        if (e == null)
            i = 0;
        return i;
    }
}